#安装python及pip

cd 

#在当前目录中创建一个虚拟环境
python3 -m venv homeassistant
#打开虚拟环境
cd homeassistant
#激活虚拟环境
source bin/activate
#安装wheel
python3 -m pip install wheel
#安装Home Assistant
python3 -m pip install homeassistant

#设置自启动运行(默认是参照官网，是在Python的虚拟环境（virtualenv）中安装的HomeAssistant)
#其他方式安装请参考https://www.hachina.io/docs/1854.html
cd /etc/systemd/system/
echo "[Unit]">>home-assistant@root.service
echo "Description=Home Assistant">>home-assistant@root.service
echo "After=network.target">>home-assistant@root.service
echo " ">>home-assistant@root.service
echo "[Service]">>home-assistant@root.service
echo "Type=simple">>home-assistant@root.service
echo "User=%i">>home-assistant@root.service
echo "ExecStart=/root/homeassistant/bin/hass -c \"/root/.homeassistant\"">>home-assistant@root.service
echo " ">>home-assistant@root.service
echo "[Install]">>home-assistant@root.service
echo "WantedBy=multi-user.target">>home-assistant@root.service
echo "#Or run Home Assistant manually">>home-assistant@root.service
echo "hass --open">>home-assistant@root.service

#文件原内容
#[Unit]
#Description=Home Assistant
#After=network.target
# 
#[Service]
#Type=simple
#User=%i
#ExecStart=/usr/bin/hass
# 
#[Install]
#WantedBy=multi-user.target

#设置自启动
systemctl enable home-assistant@root

#启动Home Assistant
systemctl start home-assistant@root

#查看日志输出
journalctl -f -u home-assistant@root
